// pages/login/login.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid: '',
    http:''
  },
  onLoad:function(){
    this.setData({
      http:app.globalData.http
    })
  },
  getUserInfo(e) {
    let http=this.data.http;
    console.log("用户登录...");
    wx.login({
      //获取code
      success: function (res) {
        wx.setStorageSync('userInfo', e.detail.rawData);
        var code = res.code; //返回code
        console.log(code);
        var appId = 'wx5a41c83a4b7df5e9';
        var secret = '4416a6e2007fce6013ea6cf430e2b111';
        wx.request({
          url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appId + '&secret=' + secret + '&js_code=' + code + '&grant_type=authorization_code',
          data: {},
          header: {
            'content-type': 'json'
          },
          success: function (res) {
            console.log(res);
            let userInfo = typeof wx.getStorageSync('userInfo')==='string'? JSON.parse(wx.getStorageSync('userInfo')):wx.getStorageSync('userInfo');
            console.log(userInfo);
            var openid = res.data.openid //返回openid
            console.log('openid为' + openid);
            wx.request({
              url: http+'/user/userinfo',
              data: {
                user_name:userInfo.nickName,
                open_id:openid
              },
              success: (result) => {
                console.log(result);
                wx.setStorageSync('user', result.data.data);
                if(result.data.data.user_phone===null){
                  console.log(1111);
                  wx.navigateTo({
                    url: '../../pages/phoneNum/phoneNum'
                  })
                }else{
                  wx.reLaunch({
                    url: '../../pages/index/index',
                  });
                }

              },
              fail: (res) => {},
              complete: (res) => {},
            })
          }
        })
      }
    })


  },

});